<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="生产任务">
      {{billNo}}
    </el-form-item>
    <el-form-item label="订单日期">
      <div class="block">
        <el-date-picker
            v-model="taskTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="领料时间">
        <div class="block">
          <el-date-picker
              v-model="pickTime"
              type="datetime"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
    </el-form-item>
    <el-form-item label="补料时间">
      <div class="block">
        <el-date-picker
            v-model="supplementTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>

    <el-form-item label="退料时间">
      <div class="block">
        <el-date-picker
            v-model="returnTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>

    <el-form-item label="变品时间" >
        <div class="block">
          <el-date-picker
              v-model="desaltingTime"
              type="datetime"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
    </el-form-item>

    <el-form-item label="入库时间">
      <div class="block">
        <el-date-picker
            v-model="prdInTime"
            type="datetime"
            placeholder="选择日期时间">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item >
      <el-checkbox v-model="checked">只传调拨单</el-checkbox>
      <el-checkbox v-model="checkTask" disabled >同品不同仓库</el-checkbox>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即上传</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import dayjs from "dayjs";

export default {
  data() {
    return {
      taskTime:'',
      pickTime: '',
      supplementTime: '',
      returnTime: '',
      desaltingTime: '',
      prdInTime: '',
      billNo: '',
      loading: false,
      type:'',
      createTime:'',
      checked: false,
      checkTask : false //同品通批次入到不同的仓库
    }
  },
  async mounted() {
    this.billNo = this.$route.query.billNo;
    this.type = this.$route.query.type;
    this.createTime = this.$route.query.createTime;
    this.checkPrdTask();
  },
  methods: {
    formatDate(date) {
      if (!date) return ''
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    async uploadKingdee(){
      this.loading = true
      try{
        let res = await this.callApi('upload_kingdee_task',{
          taskTime:dayjs(this.taskTime).format('YYYY-MM-DD HH:mm:ss'),
          pickTime:dayjs(this.pickTime).format('YYYY-MM-DD HH:mm:ss'),
          supplementTime:dayjs(this.supplementTime).format('YYYY-MM-DD HH:mm:ss'),
          returnTime:dayjs(this.returnTime).format('YYYY-MM-DD HH:mm:ss'),
          desaltingTime:dayjs(this.desaltingTime).format('YYYY-MM-DD HH:mm:ss'),
          prdInTime:dayjs(this.prdInTime).format('YYYY-MM-DD HH:mm:ss'),
          billNo:this.billNo,
          type:this.type,
          createTime:this.createTime,
          checked: this.checked
        })
        if (!res.res){
          return
        }
        this.$message.success(`上传成功`)
        setTimeout(()=>{
          window.opener.location.reload()
          window.close()
        },3000)
      } finally {
        this.loading = false
      }
    },
    async onSubmit() {
      //校验车间收货是否都入库
      this.loading = true
      let checkRes = true;
      try{
        let res =  await this.callApi('check_prd_col_in_stock',{billNo : this.billNo})
        checkRes = res.data;
      } finally {
        this.loading = false
      }
      if (!checkRes){
        this.$message.warning('存在没有入库的车间收货单')
        return
      }
  
      this.$confirm('是否确认上传金蝶', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '正在上传!'
        });
        this.uploadKingdee()
      })
    },
    // async checkPrdColIstock(){
    //   this.loading = true
    //   try{
    //     let res =  this.callApi('check_prd_col_in_stock',{billNo : this.billNo})
    //     console.log(res.data)
    //     if (res.data){
    //       return true
    //     } else {
    //       return false
    //     }
    //     return res.data
    //   } finally {
    //     this.loading = false
    //   }
    // },
    //校验有没有同品通批次入到不通仓库的
    async checkPrdTask(){
      this.loading = true
      try{
        let res =  await this.callApi('check_prd_task',{billNo : this.billNo})
        console.log(res.data)
        this.checkTask = res.data
      } finally {
        this.loading = false
      }


    }
  }
}
</script>

<style scoped lang="scss">

</style>
